﻿<%@ Control Language="C#" AutoEventWireup="true"
    Inherits="Pages_Operators_Ascx_ServiceProviderPopup" Codebehind="ServiceProviderPopup.ascx.cs" %>
<div id="serviceProvider-dialog" title="The provider of Services">
    <table cellspacing="10" class="popup">
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Provider #:</div>
                    <div class="field">
                        <asp:HiddenField ID="hidServiceProviderID" runat="server" />
                        <asp:HiddenField ID="hidProviderID" runat="server" />
                        <asp:DropDownList ID="ddlProviderNumberInput" runat="server">
                        </asp:DropDownList>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
                            ControlToValidate="ddlProviderNumberInput"></asp:RequiredFieldValidator>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <div class="label">
                        Name:</div>
                    <div class="field">
                        <asp:TextBox ID="txtProviderNameInput" runat="server" Enabled="false"></asp:TextBox>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <fieldset>
                    <legend>Method options:</legend>
                    <table id="tableMethods" cellspacing="10">
                        <tr>
                            <td>
                                <asp:RadioButton GroupName="choiceOptions" ID="rdoPackage" runat="server" />Package
                                <span id="spanSubPackage">for
                                    <asp:TextBox ID="txtNumInPackage" runat="server" Width="19" MaxLength="3" CssClass="integer"></asp:TextBox>
                                    <asp:DropDownList ID="ddlUnit" runat="server" Width="68" CssClass="subUnit">
                                    </asp:DropDownList>
                                    <span class="spanSubOtherUnit">(Unit is
                                        <asp:TextBox ID="txtOtherUom" runat="server" Width="68" Text="pack" CssClass="subUOM"></asp:TextBox>)</span>
                                    with price is
                                    <asp:TextBox ID="txtPackagePrice" runat="server" Width="39" CssClass="currency"></asp:TextBox>
                                    - The next one with price is
                                    <asp:TextBox ID="txtUnitPrice" runat="server" Width="39" CssClass="currency"></asp:TextBox>.
                                    <span style="font-style: italic">(Unit price is
                                        <asp:DropDownList ID="ddlUnitCurrency" runat="server" Width="68" />
                                        )</span> </span>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:RadioButton GroupName="choiceOptions" ID="rdoStdCost" runat="server" />Standard
                                Cost <span id="spanSubStdCost">
                                    <asp:TextBox ID="txtStandardCost" runat="server" Width="39" CssClass="currency"></asp:TextBox>
                                    <asp:DropDownList ID="ddlCurrency" runat="server" Width="68" />
                                    for
                                    <asp:DropDownList ID="ddlUnit1" runat="server" Width="68" CssClass="subUnit">
                                    </asp:DropDownList>
                                    <span class="spanSubOtherUnit">unit is
                                        <asp:TextBox ID="txtOtherUomInput" runat="server" Width="68" Text="pack" CssClass="subUOM"></asp:TextBox></span>
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <td style="color: Green">
                                <asp:CheckBox ID="chkNightFlag" runat="server" Text="Multiply this to the number of nights" />
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
        </tr>
        <tr>
            <td>
                <div class="item">
                    <span id="litNoticePopup">
                        <asp:Literal ID="litNoticePopup" runat="server" />
                    </span>
                </div>
            </td>
        </tr>
    </table>
</div>

<script type="text/javascript">

    $("#<%=ddlProviderNumberInput.ClientID %>").change(function() { changeProviderNumber(); });

    function changeProviderNumber() {
        $("#<%=hidProviderID.ClientID %>").val($("#<%=ddlProviderNumberInput.ClientID %> option:selected").attr('ProviderID'));
        $("#<%=txtProviderNameInput.ClientID %>").val($("#<%=ddlProviderNumberInput.ClientID %> option:selected").val());
    }

    function clearSubControl() {
        $("#<%=hidServiceProviderID.ClientID %>").val('');
        $("#<%=hidServiceProviderID.ClientID %>").removeAttr('index');
        $('#<%=rdoPackage.ClientID %>').attr('checked', 'checked');

        enableSubControl();
    }

    function enableSubControl() {
        if ($("#<%=rdoPackage.ClientID %>").is(':checked')) {
            $("#spanSubPackage").css('display', '');
            $("#spanSubStdCost").css('display', 'none');
        }
        else if ($("#<%=rdoStdCost.ClientID %>").is(':checked')) {
            $("#spanSubPackage").css('display', 'none');
            $("#spanSubStdCost").css('display', '');
        }
        chooseSubUnit();
    }

    $('#<%=rdoPackage.ClientID %>').click(function() { enableSubControl() });
    $('#<%=rdoStdCost.ClientID %>').click(function() { enableSubControl() });
    $("#<%=ddlUnit.ClientID %>").change(function() { chooseSubUnit(); });
    $("#<%=ddlUnit1.ClientID %>").change(function() { chooseSubUnit(); });
    $(".subUnit").blur(function() {
        var element = this;
        $(".subUnit").each(function() {
            if (this != element) {
                $(this).val($(element).val());
            }
        });
    });
    $(".subUOM").blur(function() {
        var element = this;
        $(".subUOM").each(function() {
            if (this != element) {
                $(this).val($(element).val());
            }
        });
    });

    function chooseSubUnit() {
        if (parseInt($("#<%=ddlUnit1.ClientID %> option:selected").val()) == 2) {
            $(".spanSubOtherUnit").css('display', '');
        }
        else {
            $(".spanSubOtherUnit").css('display', 'none');
        }
    }

    // call changeProviderNumber
    changeProviderNumber();

    function insertServiceProvider() {
        var tbody = $('#gridServiceProvider > tbody');
        var seq = ($('tr', tbody).length + 1);
        var evenClass = (seq % 2 == 0 ? ' class="evenRow"' : '');

        try {
            var buttons = '<a serviceproviderid="' + $("#<%=hidProviderID.ClientID %>").val() + '" class="btnEdit new">Edit</a>';
            buttons += '|&nbsp;<a serviceproviderid="' + $("#<%=hidProviderID.ClientID %>").val() + '" class="btnDelete new">Delete</a>';

            var choiceOption = $("#<%=rdoPackage.ClientID %>").is(':checked') ? 0 : 1;
            var numOfPax = 0;
            var unit = 0;
            var packagePrice = 0;
            var unitPrice = 0;
            var currency = 0;
            var uom = "";
            var method = "";
            var nightFlag = $("#<%=chkNightFlag.ClientID %>").is(":checked");
            if (choiceOption == 0) {
                numOfPax = $("#<%=txtNumInPackage.ClientID %>").val();
                unit = $("#<%=ddlUnit.ClientID %> option:selected").val();
                packagePrice = $("#<%=txtPackagePrice.ClientID %>").val();
                unitPrice = $("#<%=txtUnitPrice.ClientID %>").val();
                currency = $("#<%=ddlUnitCurrency.ClientID %> option:selected").val();
                method = stringformat("Package for {0} {1} with price is {2}{4} - The next one with price is {3}{4}",
                                                   numOfPax,
                                                   $("#<%=ddlUnit.ClientID %> option:selected").text(),
                                                   packagePrice,
                                                   unitPrice,
                                                   $("#<%=ddlUnitCurrency.ClientID %> option:selected").text());
            }
            else {
                unitPrice = $("#<%=txtStandardCost.ClientID %>").val();
                unit = $("#<%=ddlUnit1.ClientID %> option:selected").val();
                currency = $("#<%=ddlCurrency.ClientID %> option:selected").val();
                uom = $("#<%=txtOtherUomInput.ClientID %>").val();
                method = stringformat("Standard cost is {0}{1} for {2}.",
                                                   unitPrice,
                                                   $("#<%=ddlCurrency.ClientID %> option:selected").text(),
                                                   $("#<%=ddlUnit1.ClientID %> option:selected").text());
            }

            var row = stringformat('<tr{0}><td align="center">{1}</td>' +
                            '<td style="display: none">{2}</td>' +
                            '<td style="display: none">{3}</td>' +
                            '<td>{4}</td>' +
                            '<td>{5}</td>' +
                            '<td>{6}</td>' +
                            '<td style="display: none">{7}</td>' +
                            '<td style="display: none">{8}</td>' +
                            '<td style="display: none">{9}</td>' +
                            '<td style="display: none">{10}</td>' +
                            '<td style="display: none">{11}</td>' +
                            '<td style="display: none">{12}</td>' +
                            '<td style="display: none">{13}</td>' +
                            '<td style="display: none">{14}</td>' +
                            '<td>{15}</td></tr>',
                            evenClass,
                            seq,
                            "",
                            $("#<%=hidProviderID.ClientID %>").val(),
                            $("#<%=ddlProviderNumberInput.ClientID %> option:selected").text(),
                            $("#<%=txtProviderNameInput.ClientID %>").val(),
                            method,
                            choiceOption,
                            numOfPax,
                            unit,
                            packagePrice,
                            unitPrice,
                            currency,
                            nightFlag,
                            uom,
                            buttons);
            if ($.trim(tbody.html()) == '') {
                tbody.html(row);
            }
            else {
                $('tr:last', tbody).after(row);
            }

            setEvents();

            $("#litNoticePopup").text("INSERT SUCCESSFUL.");
        }
        catch (e) {
            $("#litNoticePopup").text("INSERT FAILS.");
        }
    }

    function updateServiceProvider() {
        try {
            var choiceOption = $("#<%=rdoPackage.ClientID %>").is(':checked') ? 0 : 1;
            var nightFlag = $("#<%=chkNightFlag.ClientID %>").is(":checked");
            var numOfPax = 0;
            var unit = 0;
            var packagePrice = 0;
            var unitPrice = 0;
            var currency = 0;
            var uom = "";
            var method = "";
            if (choiceOption == 0) {
                numOfPax = $("#<%=txtNumInPackage.ClientID %>").val();
                unit = $("#<%=ddlUnit.ClientID %> option:selected").val();
                packagePrice = $("#<%=txtPackagePrice.ClientID %>").val();
                unitPrice = $("#<%=txtUnitPrice.ClientID %>").val();
                currency = $("#<%=ddlUnitCurrency.ClientID %> option:selected").val();
                method = stringformat("Package for {0} {1} with price is {2}{4} - The next one with price is {3}{4}",
                                                   numOfPax,
                                                   $("#<%=ddlUnit.ClientID %> option:selected").text(),
                                                   packagePrice,
                                                   unitPrice,
                                                   $("#<%=ddlUnitCurrency.ClientID %> option:selected").text());
            }
            else {
                unitPrice = $("#<%=txtStandardCost.ClientID %>").val();
                unit = $("#<%=ddlUnit1.ClientID %> option:selected").val();
                currency = $("#<%=ddlCurrency.ClientID %> option:selected").val();
                uom = $("#<%=txtOtherUomInput.ClientID %>").val();
                method = stringformat("Standard cost is {0}{1} for {2}.",
                                                   unitPrice,
                                                   $("#<%=ddlCurrency.ClientID %> option:selected").text(),
                                                   $("#<%=ddlUnit1.ClientID %> option:selected").text());
            }

            var line = $("#gridServiceProvider > tbody > tr:eq(" + $("#<%=hidServiceProviderID.ClientID %>").attr('index') + ")");

            $(line).children("td:eq(2)").text($("#<%=hidProviderID.ClientID %>").val());
            $(line).children("td:eq(3)").text($("#<%=ddlProviderNumberInput.ClientID %>").text());
            $(line).children("td:eq(4)").text($("#<%=ddlProviderNumberInput.ClientID %>").val());
            $(line).children("td:eq(5)").text(method);
            $(line).children("td:eq(6)").text(choiceOption);
            $(line).children("td:eq(7)").text(numOfPax);
            $(line).children("td:eq(8)").text(unit);
            $(line).children("td:eq(9)").text(packagePrice);
            $(line).children("td:eq(10)").text(unitPrice);
            $(line).children("td:eq(11)").text(currency);
            $(line).children("td:eq(12)").text(nightFlag);
            $(line).children("td:eq(13)").text(uom);

            $("#litNoticePopup").text("UPDATE SUCCESSFUL.");

            $("#serviceProvider-dialog").dialog("close");
        }
        catch (e) {
            $("#litNoticePopup").text("UPDATE FAILS.");
        }
    }

    $("#serviceProvider-dialog").dialog({
        autoOpen: false,
        width: 780,
        modal: true,
        buttons: {
            "OK": function() {
                var isNew = ($("#<%=hidServiceProviderID.ClientID %>").attr('index') == undefined);
                if (isNew) {
                    insertServiceProvider();
                }
                else {
                    updateServiceProvider();
                }
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        },
        close: function() {
        }
    });

    function editProvider(idx) {
        var line = $("#gridServiceProvider > tbody > tr:eq(" + idx + ")");
        $("#<%=hidServiceProviderID.ClientID %>").attr('index', idx);
        $("#<%=hidServiceProviderID.ClientID %>").val($(line).children("td:eq(1)").text());
        $("#<%=hidProviderID.ClientID %>").val($(line).children("td:eq(2)").text());
        $("#<%=ddlProviderNumberInput.ClientID %>").val(parseInt($(line).children("td:eq(3)").text()));
        var nightFlag = $(line).children("td:eq(12)").text();
        if (nightFlag == 'true') {
            $("#<%=chkNightFlag.ClientID %>").attr('checked', 'checked');
        }
        else {
            $("#<%=chkNightFlag.ClientID %>").removeAttr('checked');
        }

        var choiceOption = parseInt($(line).children("td:eq(6)").text());
        if (choiceOption == 0) {
            $("#<%=rdoStdCost.ClientID %>").removeAttr('checked');
            $("#<%=rdoPackage.ClientID %>").attr('checked', 'checked');

            $("#<%=txtNumInPackage.ClientID %>").val(parseInt($(line).children("td:eq(7)").text()));
            $("#<%=ddlUnit.ClientID %>").val(parseInt($(line).children("td:eq(8)").text()));
            $("#<%=txtPackagePrice.ClientID %>").val($(line).children("td:eq(9)").text());
            $("#<%=txtUnitPrice.ClientID %>").val($(line).children("td:eq(10)").text());
            $("#<%=ddlUnitCurrency.ClientID %>").val(parseInt($(line).children("td:eq(11)").text()));
        }
        else {
            $("#<%=rdoPackage.ClientID %>").removeAttr('checked');
            $("#<%=rdoStdCost.ClientID %>").attr('checked', 'checked');

            $("#<%=ddlUnit1.ClientID %>").val(parseInt($(line).children("td:eq(8)").text()));
            $("#<%=txtStandardCost.ClientID %>").val($(line).children("td:eq(10)").text());
            $("#<%=ddlCurrency.ClientID %>").val(parseInt($(line).children("td:eq(11)").text()));
            $("#<%=txtOtherUomInput.ClientID %>").val($(line).children("td:eq(13)").text());
        }
    }
</script>

